package com.meihua.baoleyi.utils;

import com.lzy.okgo.OkGo;
import com.lzy.okgo.callback.AbsCallback;
import com.lzy.okgo.model.HttpParams;
import java.io.File;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
import okhttp3.Call;
import okhttp3.Response;

/**
 * Created by liming on 2017/10/16
 */

public class HttpUtil {
    // public static final String ResultDatas(HttpParams params, String url, final Callback callback){
    public static final String ResultDatas(Map<String,String> map, String url, final Callback callback){
        //  OkGo.post(url).params(map).execute(new AbsCallback<String>() {
        OkGo.post(url).params(map).execute(new AbsCallback<String>() {
            //解析网络返回的数据回调
            @Override
            public String convertSuccess(Response response) throws Exception {
                return null;
            }
            //网络请求成功的回调
            @Override
            public void onSuccess(String s, Call call, Response response) {
                try {
                    String data=response.body().string();
//                            String json=SymmetricEncoder1.decrypt(data);  //解密
                    callback.callback(data);
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            //网络请求失败的回调
            @Override
            public void onError(Call call, Response response, Exception e) {
                super.onError(call, response, e);
                try {
                    String datas=response.body().string();
                } catch (IOException e1) {
                    e1.printStackTrace();
                }
            }
        });
        return null;
    }

    public interface Callback{
        void callback(String data);
    }

    public static final String ResultDate(HashMap<String, String> params, HashMap<String,File> fileMap, String url, final Callbacke callback){
        HttpParams fileparams = new HttpParams();
        for (Map.Entry<String, File> entry : fileMap.entrySet()) {
            fileparams.put(entry.getKey(),entry.getValue());
        }
        OkGo.post(url)
                .params(params)
                .params(fileparams)
                .execute(new AbsCallback<String>() {
            //解析网络返回的数据回调
            @Override
            public String convertSuccess(Response response) throws Exception {
                return null;
            }
            //网络请求成功的回调
            @Override
            public void onSuccess(String s, Call call, Response response) {
                try {
                    String data=response.body().string();
                    callback.callback(data);
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            //网络请求失败的回调
            @Override
            public void onError(Call call, Response response, Exception e) {
                super.onError(call, response, e);
                try {
                    String datas=response.body().string();
                } catch (IOException e1) {
                    e1.printStackTrace();
                }
            }
        });
        return null;
    }

    public interface Callbacke{
        void callback(String data);
    }

















}
